package com.tangyee.home_blood_center.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.tangyee.home_blood_center.bean.BSRecord;
import com.tangyee.home_blood_center.db.BSRecordHolder;
import com.tangyee.home_blood_center.view.CircleView;
import com.tangyee.home_blood_center.view.HbAlcRecordRowView;
import com.tangyee.R;
import com.tangyee.home_blood_center.bean.BSRecord;
import com.tangyee.home_blood_center.db.BSRecordHolder;
import com.tangyee.home_blood_center.interfaces.OnHbAlcRecordClickListener;
import com.tangyee.home_blood_center.view.CircleView;
import com.tangyee.home_blood_center.view.HbAlcRecordRowView;

import java.util.ArrayList;
import java.util.List;

/**
 * 和BloodPressureFragment差不多，显示30条记录的表格
 */
public class HbAlcFragment extends  BaseFragment{
    private List<BSRecord> mBSRecords;
    private List<HbAlcRecordRowView> mHbAlcRecordRowViews;
    private CircleView mCircleView1;
    private CircleView mCircleView2;
    private CircleView mCircleView3;
    private int low;
    private int good;
    private int high;

    public static HbAlcFragment newInstance(String text){
        Bundle bundle = new Bundle();
        bundle.putString("text",text);
        HbAlcFragment hbAlcFragment = new HbAlcFragment();
        hbAlcFragment.setArguments(bundle);
        return  hbAlcFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_glycated_hemoglobin_list, container, false);

        LinearLayout linearLayout = view.findViewById(R.id.HbAlc_record_table);
        mHbAlcRecordRowViews = new ArrayList<>();
        mBSRecords = BSRecordHolder.get(getContext()).getBSRecords(30);
        int n = mBSRecords.size();
        low = good = high = 0;
        for (int i = 0; i < n; i ++) {
            // 统计信息
            BSRecord record = mBSRecords.get(i);
            analyzeData(6.0, 7.0, record.getSaccharificationValue());

            // 构建BSRecordRow
            HbAlcRecordRowView hbAlcRecordRowView = new HbAlcRecordRowView(getContext());
            hbAlcRecordRowView.setBSRecord(mBSRecords.get(i));
            hbAlcRecordRowView.setOnHbAlcRecordClickListener(new OnHbAlcRecordClickListener() {
                @Override
                public void OnTextView1Click(BSRecord bsRecord) {
                    Long time = bsRecord.getDate().getTime();
                    startAddDataFragment(time);
                }

            });
            mHbAlcRecordRowViews.add(hbAlcRecordRowView);
            linearLayout.addView(hbAlcRecordRowView, 0);
        }

        mCircleView1 = view.findViewById(R.id.HbAlc_info_left);
        mCircleView2 = view.findViewById(R.id.HbAlc_info_middle);
        mCircleView3 = view.findViewById(R.id.HbAlc_info_right);
        int total = low + good + high;
        mCircleView1.setDate(low, total);
        mCircleView2.setDate(good, total);
        mCircleView3.setDate(high, total);

        return view;
    }

    private void analyzeData(double low, double high, double data) {
        if (data <= 0) {
            return;
        }
        if (data < low) {
            this.low ++;
            return;
        }
        if (data > high) {
            this.high ++;
            return;
        }
        good ++;
    }


    private void startAddDataFragment(Long time) {
        AddHbAlcRecordFragment addHbAlcRecordFragment = AddHbAlcRecordFragment.newInstance(time);
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        List<Fragment> fragments = fm.getFragments();
        transaction.hide(fragments.get(0));
        transaction.setCustomAnimations(R.anim.move_right_in_activity, R.anim.move_right_out_activity, R.anim.move_right_out_activity, R.anim.move_left_out_activity) // 瞎设置
                .add(R.id.fragment_container, addHbAlcRecordFragment, "addHbAlcRecordFragment")
                .addToBackStack("addHbAlcRecordFragment")
                .commit();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //相当于Fragment的onResume
            updateUI();
        } else {
            //相当于Fragment的onPause
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        updateUI();
    }

    private void updateUI() {
        if (mHbAlcRecordRowViews.size() == 0) {
            return;
        }
        low = high = good = 0;
        mBSRecords = BSRecordHolder.get(getContext()).getBSRecords(30);
        for (int i = 0; i < mBSRecords.size(); i ++) {
            // 统计信息
            BSRecord record = mBSRecords.get(i);
            analyzeData(6.0, 7.0, record.getSaccharificationValue());

            // 重设数据
            mHbAlcRecordRowViews.get(i).setBSRecord(record);
        }
        int total = low + good + high;
        mCircleView1.setDate(high, total);
        mCircleView2.setDate(good, total);
        mCircleView3.setDate(low, total);
    }

}
